package com.asa.appbarrecycleview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso

/**
 * create By：anderson
 * on 2022/3/12
 * desc:
 */
class FeedAdapter:RecyclerView.Adapter<FeedAdapter.FeedHolder>() {

    class FeedHolder(itemView:View) : RecyclerView.ViewHolder(itemView){
         var ivContent:ImageView
         var ivAvatar:ImageView
        var tvNickname:TextView
        init {
             ivContent = itemView.findViewById(R.id.iv_content)
             ivAvatar = itemView.findViewById(R.id.iv_avatar)
             tvNickname = itemView.findViewById(R.id.tv_nickname)
        }


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedHolder {
        return FeedHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_feed,parent,false))
    }

    override fun onBindViewHolder(feedHolder: FeedHolder, position: Int) {
        //用户头像
        Picasso.with(feedHolder.itemView.getContext())
            .load(getAvatarResId(position))
            .centerInside()
            .fit()
            .into(feedHolder.ivAvatar)

        //内容图片
        Picasso.with(feedHolder.itemView.getContext())
            .load(getContentResId(position))
            .centerInside()
            .fit()
            .into(feedHolder.ivContent)

        //nickname
        feedHolder.tvNickname.setText("NetEase $position")
    }

    private fun getAvatarResId(position: Int): Int {
        when (position % 4) {
            0 -> return R.drawable.avatar1
            1 -> return R.drawable.avatar2
            2 -> return R.drawable.avatar3
            3 -> return R.drawable.avatar4
        }
        return 0
    }

    fun getContentResId(position: Int): Int {
        when (position % 4) {
            0 -> return R.drawable.taeyeon_one
            1 -> return R.drawable.taeyeon_two
            2 -> return R.drawable.taeyeon_three
            3 -> return R.drawable.taeyeon_four
        }
        return 0
    }

    override fun getItemCount(): Int {
       return 100
    }

}